<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>

<style>
    .layui-table-cell {
        height: 100px;
        width: 150px;
    }

    img {
        width: auto;
        height: 100%;
    }
</style>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">新闻编号</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="id" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">发布者</label></label>
                                <div class="layui-input-inline">
                                    <input type="text" name="adminUserName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">文章标题</label></label></label></label>
                                <div class="layui-input-inline">
                                    <input type="text" name="title" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">发布时间</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="time" id="time" placeholder="yyyy-MM-dd" autocomplete="off"
                                        class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
<!--                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>-->
<!--                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>-->
<!--                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>-->
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

            <script type="text/html" id="picTpl">
            <!-- 这里的 checked 的状态只是演示 -->
            <img src="{{d.img}}" name="img" alt="" width="100%" height="100%">
            </script>


        </div>
    </div>
    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table', 'laydate'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                laydate = layui.laydate;

            //日期
            laydate.render({
                elem: '#time'
            });

            table.render({
                elem: '#currentTableId',
                url: 'http://localhost:8080/api/news/pageDataNews',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [[
                    { type: "checkbox", fixed: 'left' },
                    { field: 'id', width: 80, title: 'ID', sort: true },
                    { field: 'adminUserName', width: 120, title: '作者' },
                    { field: 'title', minWidth: 200, title: '标题' },
                    { field: 'img', title: '封面', minWidth: 100, templet: '#picTpl' },
                    { field: 'time', minWidth: 80, title: '发布时间', sort: true },
                    // { field: 'experience', width: 80, title: '积分', sort: true },
                    // { field: 'score', width: 80, title: '评分', sort: true },
                    // { field: 'classify', width: 80, title: '职业' },
                    // { field: 'wealth', width: 135, title: '财富', sort: true },
                    { title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center" }
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                var result = JSON.stringify(data.field);
                layer.alert(result, {
                    title: '最终的搜索信息'
                });

                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        // searchParams: result
                        "id": data.field.id,
                        "adminUserName": data.field.adminUserName,
                        "title": data.field.title,
                        "time": data.field.time
                    }
                }, 'data');

                return false;
            });


            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);

                // console.log(obj);
                if (obj.event === 'getCheckData') {

                    var data = checkStatus.data;
                    // console.log(data[0]);
                    layer.alert(JSON.stringify(data));

                } else if (obj.event === 'getCheckLength') {

                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');

                } else if (obj.event === 'isAll') {

                    layer.msg(checkStatus.isAll ? '全选' : '未全选');

                } else if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加新闻',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: './addNews.html',
                        end: function () {
                            table.reload("currentTableId", 'data')
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作

                    var data = checkStatus.data;

                    if (0 === data.length) {
                        return;
                    }

                    for (let index = 0; index < data.length; index++) {
                        const element = data[index];
                        let id = element.id;
                        $.post('http://localhost:8080/api/news/deleteNews'
                            , { id: id });
                    }
                    layer.msg("删除成功");


                    //执行搜索重载
                    table.reload('currentTableId', 'data');

                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                console.log(obj);
                if (obj.event === 'edit') {

                    localStorage.setItem("newsId",data.id);
                    var index = layer.open({
                        title: '修改新闻信息',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: './modifyNews.html',
                        end:function() {
                            table.reload('currentTableId', 'data');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    layer.confirm('真的删除行么', function (index) {
                        // obj.del();
                        // console.log(index);
                        // console.log(data);

                        let id = data.id;
                        console.log(id);
                        $.post('http://localhost:8080/api/news/deleteNews',{
                            id: id
                        },(data,status) => {
                            if (status === "success" && data.code === 0) {
                                layer.close(index);
                                //执行搜索重载
                                table.reload('currentTableId', 'data');
                                layer.msg("删除成功");
                            } else {
                                layer.close(index);
                                layer.alert("删除失败")
                            }

                        });

                    });
                }
            });

        });
    </script>

</body>

</html>